package servlet;

import model.Page;
import service.GoodsService;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "goodsrecommend_list", value = "/goodsrecommend_list")
public class GoodRecommendListServlet extends HttpServlet {
    private GoodsService gService = new GoodsService();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int type = Integer.parseInt(request.getParameter("type") ) ;
        int pageNumber = 1;
        if(request.getParameter("pageNumber") != null) {
            try {
                pageNumber=Integer.parseInt(request.getParameter("pageNumber") ) ;
            } catch (Exception e) {
            }
        }
        if(pageNumber<=0)
            pageNumber=1;
        //对获取到的热销商品进行分页
        Page p = gService.getGoodsRecommendPage(type, pageNumber);

        if(p.getTotalPage()==0) {
            p.setTotalPage(1);
            p.setPageNumber(1);
        } else {
            //根据查询到的商品数量进行分页
            if(pageNumber>=p.getTotalPage()+1) {
                p = gService.getGoodsRecommendPage(type, p.getTotalPage());
            }
        }
        request.setAttribute("p", p);
        request.setAttribute("t", type);
        request.getRequestDispatcher("goodsrecommend_list.jsp").forward(request, response);
    }
}
